package com.yuxl.common.auth.repository;

import com.yuxl.common.auth.entity.AuthMenu;
import com.yuxl.common.core.annotation.PostgreSqlRepository;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Author : yuxl
 * @create 2025/7/22 下午5:08
 */
@PostgreSqlRepository
public interface AuthMenuRepository extends ReactiveCrudRepository<AuthMenu, String> {
    @Query("""
            SELECT count(*) FROM auth_menu
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            """)
    Mono<Integer> count(AuthMenu entity);

    @Query("""
            SELECT * FROM auth_menu
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            ORDER BY create_time DESC
            LIMIT :limit OFFSET :offset
            """)
    Flux<AuthMenu> page(AuthMenu entity, long limit, long offset);

    @Query("""
            SELECT * FROM auth_menu
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            ORDER BY create_time DESC
            """)
    Flux<AuthMenu> list(AuthMenu entity);


    // 添加根据id 的 update语句
    @Query("""
            UPDATE auth_menu
            SET update_time = :#{#entity.updateTime}
            WHERE name = :#{#entity.name}
            RETURNING *
            """)
    Mono<AuthMenu> updateById(AuthMenu entity);
}
